package com.sita.tingterest.services;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import com.sita.tingterest.utils.L;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BoundServices extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener {
    public static String LOG_TAG = "BoundService";

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }

    //media player
    public int totalTime = 0;


    public int CurrentPosition = 0;
    private int mPlayingPosition;
    private MediaPlayer player;
    private String SongUri;
    private IBinder mBinder = new MyBinder();
    private ExecutorService mProgressUpdatedListener = Executors.newSingleThreadExecutor();

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (player.getCurrentPosition() > 0) {
            player = mp;
            player.reset();
            Log.v(LOG_TAG, "onCompletion +" + player.getCurrentPosition());
            //   playNext();
        }

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Log.v(LOG_TAG, "Playback Error");
        player.reset();
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    public boolean isPlaying() {
        L.l("MY TAG", "  isPlaying()  PlayService");
        return player != null && player.isPlaying();
    }

    public void pre() {
        L.l("MY TAG", " pre() PlayService");
    }

    public void next() {
        L.l("MY TAG", "  next()  PlayService");
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        setTotalTime(mp.getDuration());
        Log.i(LOG_TAG, "percent " + percent + "     mp.getDuration(); " + mp.getDuration() + " mp Current Position" + mp.getCurrentPosition());
    }


    public class MyBinder extends Binder {
        public BoundServices getServices() {

            return BoundServices.this;
        }
    }

    public BoundServices() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(LOG_TAG, "in onBind");
        // TODO: Return the communication channel to the service.
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(LOG_TAG, "in onCreate");

        player = new MediaPlayer();

        initMusicPlayer();
        //*/*/*/LOCAL SONG//
        //  player=MediaPlayer.create(getApplicationContext(), R.raw.song);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        SongUri = intent.getStringExtra("SongUri");
        Log.v(LOG_TAG, "   SongUri : " + SongUri);
        return START_STICKY;
    }

    public void initMusicPlayer() {
        //set player properties

        player.setWakeMode(getApplicationContext(),
                PowerManager.PARTIAL_WAKE_LOCK);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //set listeners
        player.setOnPreparedListener(this);
        player.setOnCompletionListener(this);
        player.setOnErrorListener(this);
        player.setOnBufferingUpdateListener(this);

    }

    @Override
    public void onRebind(Intent intent) {

        Log.v(LOG_TAG, "in onRebind");

        super.onRebind(intent);

    }

    @Override
    public boolean onUnbind(Intent intent) {

        Log.v(LOG_TAG, "in onUnbind +  player.stop();+  player.release(); ");

        player.stop();

        player.release();

        return true;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();

        Log.v(LOG_TAG, "in onDestroy");

        stopForeground(true);


    }

    //*/*/*/Online Song ,,,,,,,,,//
    public int playSong() {

        try {
            player.reset();

            player.setDataSource(SongUri);

            player.prepareAsync();

            //  setTotalTime(player.getDuration());

        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }


    //playback methods
    public int getCurrentPosition() {

        Log.v(LOG_TAG, " playSong(getCurrentPosition()  :" + player.getCurrentPosition());

        return player.getCurrentPosition();
    }

    public int getDuration() {

        if (!isPlaying()) return 0;

        Log.v(LOG_TAG, " playSong(player.getDuration()  :" + player.getDuration());

        return player.getDuration();
    }


    public boolean isPng() {

        return player.isPlaying();

    }

    public int pausePlayer() {
        //playSong();
        player.pause();

        Log.v(LOG_TAG, " pausePlayer() position   :" + player.getDuration() + "  == " + mPlayingPosition);

        return mPlayingPosition;

    }

    public int getPlayingPosition() {

        Log.v(LOG_TAG, " getPlayingPosition() position  :" + player.getDuration() + "  == " + mPlayingPosition);

        return mPlayingPosition;

    }

    public void seek(int posn) {

        if (!isPlaying())

            return;

        player.seekTo(posn);

    }

    public void go() {

        player.start();

    }
}
